﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserControls_Login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            LoginControl.VisibleWhenLoggedIn = true ;
        }
        LoginControl.FindControl("UserName").Focus();
        //LoginControl.FindControl("LoginButton").Focus();
    }
    
    protected void LoginControl_Authenticate1(object sender, AuthenticateEventArgs e)
    {
        string userName = LoginControl.UserName;
        string pwd = LoginControl.Password;
        DataDSTableAdapters.TAIKHOANTableAdapter tkAdapter = new DataDSTableAdapters.TAIKHOANTableAdapter();
        DataDS.TAIKHOANDataTable tkTable = tkAdapter.TAIKHOAN_LayTheoCMND(userName);

        if (tkTable.Count == 0)
        {
            LoginControl.FailureText = "Tên đăng nhập không tồn tại!";
            return;//login fail by invalid username
        }
        string db_salt = tkTable[0].Salt;
        string db_hash = tkTable[0].Hash_Pass;
        bool kq = GlobalFunctions.verifyPassword(pwd, db_hash, db_salt);


        if (kq == true)
        {
            //check for avaiable
            if(tkTable[0].TrangThai == false)
            {
                LoginControl.FailureText = "Tài khoản chưa kích hoạt";
                return;
            }
           
            //login successfully, init session      
            int role = tkTable[0].Role;
            int loginId = tkTable[0].Id;
            string CMND = tkTable[0].CMND;
            GlobalFunctions gf = new GlobalFunctions();
            gf.initSession(loginId, role, CMND);
            //Session["LoginID"] = loginTable[0].Ma;
            //Session["Role"] = loginTable[0].MaRole;
            //Session["Username"] = loginTable[0].UserName;
            e.Authenticated = true;


            //FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(1), LoginControl.RememberMeSet, loginTable[0].Ma.ToString());
            //string encryptedCookie = FormsAuthentication.Encrypt(ticket);
            //HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
            //if (LoginControl.RememberMeSet)
            //{
            //    cookie.Expires = ticket.Expiration;
            //    cookie.Path = FormsAuthentication.FormsCookiePath;
            //    Response.Cookies.Add(cookie);
            //}


            //if (Request.UrlReferrer != null)
            //{
            //    string back = Request.UrlReferrer.ToString();
            //    if (role == 1 && back.IndexOf("Admin") == -1)
            //        Response.Redirect("/SMO/ChucNang/Admin/Admin_index.aspx");
            //    Response.Redirect(back);
            //    return;
            //}
            if(Session["returnurl"] != null)            
            {
                Response.Redirect(Session["returnurl"].ToString());
                return;
            }
            switch (role)
            {
                case 3:
                    //Response.Redirect("/Admin/Admin_index.aspx");
                    //break;
                case 2:
                    Response.Redirect("/SMO/ChucNang/NguoiDung/NguoiDung_ChaoMung.aspx");
                    break;
                case 1:
                    Response.Redirect("/SMO/ChucNang/Admin/Admin_index.aspx");
                    break;
            }
        }
        LoginControl.FailureText = "Sai mật khẩu!";
        return;
    }
    
}
